package com.sunmouse.toolkit.fragment.ftp;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.sunmouse.toollib.utils.StringUtils;
import com.sunmouse.toollib.utils.ThreadPoolUtils;
import com.sunmouse.toollib.utils.ToastUtils;
import com.sunmouse.toollib.utils.system.SystemUtils;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.constant.Config;
import com.sunmouse.toolkit.constant.Value;
import com.sunmouse.toolkit.fragment.BaseFragment;
import com.sunmouse.toolkit.ftp.FTPHelper;
import com.sunmouse.toolkit.ftp.SimpleOnFTPListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class FtpConnectFragment extends BaseFragment {


    @BindView(R.id.ip_et)
    EditText ipEt;
    @BindView(R.id.port_et)
    EditText portEt;
    @BindView(R.id.username_et)
    EditText usernameEt;
    @BindView(R.id.password_et)
    EditText passwordEt;
    @BindView(R.id.connect_btn)
    Button connectBtn;
    Unbinder unbinder;

    private String ip;
    private int port;
    private String username;
    private String psw;

    private FTPHelper mFtp;

    public FtpConnectFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_ftp_connect, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {
        this.mFtp = FTPHelper.getInstance(getActivity());
        this.mFtp.setListener(mOnFTPListener);

        ip = mPersistentUtils.readString(Config.Key.IP);
        port = mPersistentUtils.readInt(Config.Key.PORT);
        username = mPersistentUtils.readString(Config.Key.USERNAME);
        psw = mPersistentUtils.readString(Config.Key.PSW);

        this.ipEt.setText(ip);
        this.portEt.setText(String.valueOf(port));
        this.usernameEt.setText(username);
        this.passwordEt.setText(psw);

        this.connectBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        ip = ipEt.getText().toString();
        String ps = portEt.getText().toString();
        username = usernameEt.getText().toString();
        psw = passwordEt.getText().toString();

        if (StringUtils.isEmpty(ip) || StringUtils.isEmpty(ps) || StringUtils.isEmpty(username) || StringUtils.isEmpty(psw)) {
            ToastUtils.simpleToast(getActivity(), "关键字段不能为空，请检查后尝试");
            return;
        }

        if (!StringUtils.isNumber(ps)) {
            ToastUtils.simpleToast(getActivity(), "端口格式错误");
            return;
        }
        SystemUtils.closeKeyboard(getActivity());
        port = Integer.parseInt(ps);

        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                mFtp.connect(ip, port, username, psw);
            }
        });
    }


    private SimpleOnFTPListener mOnFTPListener = new SimpleOnFTPListener() {
        @Override
        public void onReady() {
        }

        @Override
        public void onLogin() {
            mPersistentUtils.writeString(Config.Key.IP, ip);
            mPersistentUtils.writeInt(Config.Key.PORT, port);
            mPersistentUtils.writeString(Config.Key.USERNAME, username);
            mPersistentUtils.writeString(Config.Key.PSW, psw);

            mIntent = new Intent(Value.Action.SWITCH);
            mIntent.putExtra(Config.Key.INDEX, 1);
            getActivity().sendBroadcast(mIntent);

            mIntent = new Intent(Value.Action.BACK)
                    .putExtra(Config.Key.TYPE, Value.UPDATE);
            getActivity().sendBroadcast(mIntent);
        }

        @Override
        public void onFailure() {
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
